package com.myspring.lab.dao;

import com.myspring.lab.entity.Project;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;
import java.util.List;

public class DbProject {
    private SessionFactory sessionFactory;

    public DbProject(){
//        Configuration configuration = new Configuration();
//        configuration.configure("hibernate.cfg.xml");
//        sessionFactory = configuration.buildSessionFactory();
//        DbConnnect dbConnnect = new DbConnnect();
        sessionFactory = DbConnnect.getSessionFactory();
    }
    public boolean haveProject(Project project){
        Session session = sessionFactory.openSession();
        Query query = session.createQuery("from Project where proNo = ?1");
        query.setParameter(1,project.getProNo());
        List<Project> projects = query.list();
        if(projects.size()!=0){
            return true;
        }else{
            return false;
        }
    }
    public boolean addProject(Project project){
        if(this.haveProject(project)){
            return false;
        }
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(project);
        session.getTransaction().commit();
        session.close();
        return true;
    }

    public boolean deleteProject(Project project){
        if(!this.haveProject(project)){
            return false;
        }
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.delete(project);
        session.getTransaction().commit();
        session.close();
        return true;
    }

    public boolean modifyProject(Project project){
        if(this.haveProject(project)){
            Session session = sessionFactory.openSession();
            session.beginTransaction();
            session.saveOrUpdate(project);
            session.getTransaction().commit();
            session.close();
            return true;
        }
        return false;
    }

    public Project[] getAllProject(){
        Session session = sessionFactory.openSession();
        Query query = session.createQuery("from Project ");
        List<Project> res = query.list();
        Project[] projects = new Project[res.size()];
        res.toArray(projects);
        return projects;
    }

    public Project[]  queryProject(String projectName){
        Session session = sessionFactory.openSession();
        Query query = session.createQuery("from Project where proName like '%"+projectName+"%'");
        List<Project> res = query.list();
        Project[] projects = new Project[res.size()];
        res.toArray(projects);
        return projects;
    }

/*    public static void main(String[] args) {
        DbProject dbProject = new DbProject();
        Project project = new Project();
        project.setProNo("02");
        project.setProName("物联网科技");
        project.setProUser("张三");
        project.setProGrade("国家级");
        project.setProFee(20);
        project.setProDetail("利用物联网技术推进传统电器行业发展");
        System.out.println(dbProject.haveProject(project));
//        System.out.println(dbProject.addProject(project));
//        System.out.println(dbProject.deleteProject(project));
//        System.out.println(dbProject.modifyProject(project));
        Project[] projects = dbProject.getAllProject();
        for(Project i:projects){
            System.out.println(i.getProNo()+" "+i.getProName()+" "+i.getProDetail());
        }
    }*/
}
